package com.offcn.canal.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.offcn.common.entity.Result;
import com.offcn.content.feign.ContentFeign;
import com.offcn.content.pojo.Content;
import com.offcn.itemweb.feign.ItemWebFeign;
import com.xpand.starter.canal.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;


/**
 * Canal监听器
 *
 * @author 秋林
 * @date 2022/10/13 15:22:10
 */
@CanalEventListener
public class CanalListener {
    @Autowired
    private ContentFeign contentFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ItemWebFeign itemWebFeign;

    /**
     * 实现canal微服务远程调用content微服务，以及更新缓存
     *
     * @param eventType
     * @param rowData
     */
    @ListenPoint(eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE, CanalEntry.EventType.UPDATE}, table = {"tb_content", "tb_content_category"}, schema = "dongyimaidb", destination = "example")
    public void customMethod(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //获取当前操作关键字
        String name = eventType.name();
        if ("DELETE".equals(name)) {
            List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
            for (CanalEntry.Column column : beforeColumnsList) {
                //获取每个单元给格的数据
                String label = column.getName();
                String value = column.getValue();
                if ("category_id".equals(label)) {
                    Result<List<Content>> result = contentFeign.findByCategoryId(Long.parseLong(value));
                    List<Content> list = result.getData();
                    //redis更新
                    String json = JSON.toJSONString(list);
                    stringRedisTemplate.boundValueOps("content_" + value).set(json);
                }
            }
        } else {
            //在修改之后获取整行所有列的数据
            List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
            for (CanalEntry.Column column : afterColumnsList) {
                //获取每个单元给格的数据
                String label = column.getName();
                String value = column.getValue();
                if ("category_id".equals(label)) {
                    Result<List<Content>> result = contentFeign.findByCategoryId(Long.parseLong(value));
                    List<Content> list = result.getData();
                    //redis更新
                    String json = JSON.toJSONString(list);
                    stringRedisTemplate.boundValueOps("content_" + value).set(json);
                }
            }
        }
    }


    /**
     * 监听商品表变化，进而触发创建html页面
     *
     * @param eventType
     * @param rowData
     */
    @ListenPoint(eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE, CanalEntry.EventType.UPDATE}, table = {"tb_goods"}, schema = "dongyimaidb", destination = "example")
    public void customMethod2(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //在修改之后获取整行所有列的数据
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            //获取每个单元给格的数据
            String label = column.getName();
            String value = column.getValue();
            if ("id".equals(label)) {
                // 生成静态页
                itemWebFeign.createHtml( Long.parseLong(value));
            }
        }
    }
}
